<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item v-if="form.dealId" :label="$t('回款项目')">
            <el-input v-model="form.dealName" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('回款时间')" prop="distributionDate">
            <DatePicker
              v-model="form.distributionDate"
              :placeholder="$t('回款时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('权益变更时间')" prop="equityChangeTime">
            <DatePicker
              v-model="form.equityChangeTime"
              :disabled="true"
              :placeholder="$t('权益变更时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="!form.dealId" :label="$t('收益类型')" prop="typeFee">
            <dictSelect
              v-if="!form.dealId"
              v-model="form.typeFee"
              :clearable="true"
              dictionary="fund_income_type_fee"
            />
          </el-form-item>
        </el-col>
        <el-col v-if="!form.dealId" :span="12">
          <el-form-item v-if="form.dealId" :label="$t('回款来源')" prop="dealId">
            <el-input v-model="form.dealName" disabled />
          </el-form-item>
        </el-col>
        <el-col v-if="form.dealId" :span="12">
          <el-form-item :label="$t('本金') + $t('金额单位')" prop="amountPrincipal">
            <Money v-model="form.amountPrincipal" unit="w" :placeholder="$t('本金')" @change="amountChange" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('收益') + $t('金额单位')" prop="amountIncome">
            <Money v-model="form.amountIncome" unit="w" :placeholder="$t('收益')" @change="amountChange" />
          </el-form-item>
        </el-col>
        <el-col v-if="form.dealId" :span="12">
          <el-form-item :label="$t('分红') + $t('金额单位')" prop="amountBonus">
            <Money v-model="form.amountBonus" unit="w" :placeholder="$t('分红')" @change="amountChange" />
          </el-form-item>
        </el-col>
        <el-col v-if="form.dealId" :span="12">
          <el-form-item :label="$t('其他') + $t('金额单位')" prop="amountOthers">
            <Money v-model="form.amountOthers" unit="w" :placeholder="$t('其他')" @change="amountChange" />
          </el-form-item>
        </el-col>
        <el-col v-if="form.dealId" :span="12">
          <el-form-item :label="$t('回款总额') + $t('金额单位')" prop="amountDistribution">
            <Money v-model="form.amountDistribution" unit="w" disabled :placeholder="$t('回款总额')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('币种')" prop="currency">
            <el-select v-model="form.currency" :placeholder="$t('币种')">
              <el-option
                v-for="dict in currencyOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('收益是否已经分配给出资人')" prop="distributionLP">
            <el-radio-group v-model="form.distributionLP">
              <el-radio v-for="(dict,index) in lpOptions" :key="index" :label="dict.dictValue">{{ $t(dict.dictLabel) }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('分摊方式')" prop="shareWay">
            <el-select v-model="form.shareWay" value="" :placeholder="$t('分摊方式')" @change="shareWayChange">
              <el-option
                v-for="dict in shareWayOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="fund:income" />
    </div>
  </div>
</template>

<script>
    import { saveFundIncome, getFundIncome, findDealBase } from '@/api/fund/income/fundIncome'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 币种字典
                currencyOptions: [],
                // 系统是否
                lpOptions: [],
                // 分摊方式字典数据
                shareWayOptions: [],
                // 表单校验
                rules: {
                  equityChangeTime: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], dealId: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], distributionDate: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], typeFee: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  amountDistribution: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], shareWay: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ] },
                // 项目
                dealBaseList: []
            }
        },
        watch: {
          'form.distributionDate': {
            handler(val) {
              if (this.form.equityChangeTime === '' || this.form.equityChangeTime === undefined || this.form.equityChangeTime === null || this.form.id === undefined) {
                this.form.equityChangeTime = val
              }
            },
            deep: true,
            immediate: true
          },
          'form.equityChangeTime': {
            handler(val) {
              if (val === '' || val === undefined || val === null) {
                this.form.equityChangeTime = this.form.distributionDate
              }
            },
            deep: true,
            immediate: true
          }
        },
        created() {
          this.getDicts('currency').then(response => {
            this.currencyOptions = response.data
          })
          this.getDicts('sys_yes_no').then(response => {
            this.lpOptions = response.data
          })
          findDealBase(this.$route.query.id).then(response => {
            this.dealBaseList = response.data
          })
          this.getDicts('fund_share_way').then(response => {
            this.shareWayOptions = response.data
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getFundIncome(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                fundId: this.$route.query.id,
                dealId: undefined,
                distributionDate: undefined,
                equityChangeTime: undefined,
                typeFee: undefined,
                amountPrincipal: undefined,
                amountIncome: null,
                amountBonus: null,
                amountOthers: null,
                amountDistribution: null,
                currency: undefined,
                remark: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                distributionLP: 'N'
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveFundIncome(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          amountChange() {
            var money = (isNaN(Number(this.form.amountPrincipal)) ? 0 : Number(this.form.amountPrincipal)) +
              (isNaN(Number(this.form.amountIncome)) ? 0 : Number(this.form.amountIncome)) +
              (isNaN(Number(this.form.amountBonus)) ? 0 : Number(this.form.amountBonus)) +
              (isNaN(Number(this.form.amountOthers)) ? 0 : Number(this.form.amountOthers))
            if (!isNaN(money)) {
              this.form.amountDistribution = money
            } else {
              this.form.amountDistribution = undefined
            }
          },
          shareWayChange() {
            if (this.form.dealId === undefined && this.form.shareWay === 'TZXMBL') {
              this.msgError(this.$t('基金收益不允许选择投资项目比例'))
              this.form.shareWay = 'RJBL'
            }
          }
        }
    }
</script>
